using System;
using System.Collections.Generic;

namespace WMSSystem.Application.Workflow
{
    /// <summary>
    /// 角色映射辅助类
    /// </summary>
    public static class RoleMappingHelper
    {
        /// <summary>
        /// 角色映射字典
        /// </summary>
        private static readonly Dictionary<string, string> RoleMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            // 经理相关
            { "经理", "Manager" },
            { "manager", "Manager" },
            { "主管", "Manager" },
            { "负责人", "Manager" },
            
            // 财务相关
            { "财务", "Finance" },
            { "finance", "Finance" },
            { "会计", "Finance" },
            { "出纳", "Finance" },
            
            // 仓库管理员相关
            { "仓库", "WarehouseManager" },
            { "warehouse", "WarehouseManager" },
            { "库管", "WarehouseManager" },
            { "仓储", "WarehouseManager" },
            { "管理员", "WarehouseManager" }
        };

        /// <summary>
        /// 根据 RealName 获取角色代码
        /// </summary>
        /// <param name="realName">真实姓名/角色名称</param>
        /// <returns>角色代码</returns>
        public static string GetRoleCode(string realName)
        {
            if (string.IsNullOrEmpty(realName))
                return "Manager"; // 默认角色

            var normalizedName = realName.Trim().ToLower();
            
            // 精确匹配
            if (RoleMappings.ContainsKey(normalizedName))
                return RoleMappings[normalizedName];
            
            // 包含匹配
            foreach (var mapping in RoleMappings)
            {
                if (normalizedName.Contains(mapping.Key.ToLower()))
                    return mapping.Value;
            }
            
            // 默认返回经理角色
            return "Manager";
        }

        /// <summary>
        /// 获取角色描述
        /// </summary>
        /// <param name="roleCode">角色代码</param>
        /// <returns>角色描述</returns>
        public static string GetRoleDescription(string roleCode)
        {
            return roleCode switch
            {
                "Manager" => "经理",
                "Finance" => "财务",
                "WarehouseManager" => "仓库管理员",
                _ => "未知角色"
            };
        }

        /// <summary>
        /// 检查是否为有效的审批角色
        /// </summary>
        /// <param name="roleCode">角色代码</param>
        /// <returns>是否为有效角色</returns>
        public static bool IsValidApprovalRole(string roleCode)
        {
            return roleCode switch
            {
                "Manager" => true,
                "Finance" => true,
                "WarehouseManager" => true,
                _ => false
            };
        }
    }
} 